package com.helploan.helploangenerator;

import javax.script.*;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

/**
 * @author 小C工作室
 * @date 2022/9/21 20:33
 * @project manager_system_server
 * @company 韦伯创想
 * @description
 */
public class Demo1 {

	public static void main(String[] args) throws FileNotFoundException, ScriptException, NoSuchMethodException {
		// 获得一个 js 的执行引擎
		ScriptEngine javascript = new ScriptEngineManager().getEngineByName("javascript");
		// 建立上下文变量
		Bindings bindings = javascript.createBindings();
		bindings.put("factor", 10);
		// 绑定上下文,作用域是当前引擎范围
		javascript.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
		Scanner input = new Scanner(System.in);
		while (input.hasNextInt()) {
			int first = input.nextInt();
			int sec = input.nextInt();
			System.out.println("输入参数是：" + first + "," + sec);
			//执行js代码
			javascript.eval(new FileReader("G:\\工作文件\\外包项目\\助贷系统\\manager_system_server\\help-loan-generator\\src\\main\\java\\com\\helploan\\helploangenerator\\model.js"));
			//是否可调用方法
			if (javascript instanceof Invocable) {
				Invocable in = (Invocable) javascript;
				//执行js中的函数
				Double result = (Double) in.invokeFunction("formula", first, sec);
				System.out.println("运算结果：" + result.intValue());
			}
		}
	}
}
